//
//  RBMyGroupVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/15.
//

#import "RBMyGroupVC.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RBMyGroupListVC.h"
#import "RBNavigationBar.h"

@interface RBMyGroupVC ()

@property(nonatomic, strong)RBNavigationBar *navigationBar;
@property(nonatomic, strong)RBPagerView *pagerView;
@property(nonatomic, strong)NSMutableDictionary *vcsDicM;

@property (nonatomic, strong) UITextField *searchTF;

@end

@implementation RBMyGroupVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    self.pagerView.hidden = NO;
}

- (RBMyGroupListVC *)getVcAtIndex: (NSInteger)index
{
    RBMyGroupListVC *vc = self.vcsDicM[@(index)];
    if (!vc)
    {
        vc = RBMyGroupListVC.new;
        vc.type = index;
        vc.searchText = self.searchTF.text;
        [self addChildViewController:vc];
        self.vcsDicM[@(index)] = vc;
    }
    return vc;
}

#pragma mark - Network

#pragma mark - Event

- (void)searchBtnTapped
{
    for (RBMyGroupListVC *vc in self.vcsDicM.allValues) {
        vc.searchText = self.searchTF.text;
        [vc getFirstPageData];
    }
}

#pragma mark - Setter

#pragma mark - Getter

- (NSMutableDictionary *)vcsDicM
{
    if (!_vcsDicM)
    {
        self.vcsDicM = NSMutableDictionary.dictionary;
    }
    return _vcsDicM;
}

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *view = RBNavigationBar.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            view.needGradientView = NO;
            [view.naviBar addBackBtnWithWidth:70 image:[UIImage imageNamed:@"backBlack"]];
            
            UILabel *searchL = [RanTool labelWithFontSize:Zoom(18) weight:0 color:@"#222222" align:1 text:@"搜索"];
            [searchL rbAddTapGesWithTarget:self action:@selector(searchBtnTapped)];
            [view.naviBar addSubview:searchL];
            [searchL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.right.mas_equalTo(0);
                make.width.mas_equalTo(Zoom(60));
            }];
            
            UIView *searchView = UIView.new;
            searchView.backgroundColor = [RBThemeConfig bgGrayColor];
            [searchView rbSetCornerRadius:Zoom(18)];
            [view.naviBar addSubview:searchView];
            [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(40));
                make.width.mas_equalTo(Zoom(279));
                make.centerY.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(36));
            }];
            
            UIImageView *searchImgView = [RanTool imgViewWithMode:1 radius:0 imgName: @"homeSearchIcon"];
            [searchView addSubview:searchImgView];
            [searchImgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(14));
                make.width.height.mas_equalTo(Zoom(15));
                make.centerY.mas_equalTo(0);
            }];
            
            UITextField *textF = [UITextField new];
            textF.placeholder = @"请输入商品名称";
            textF.font = [UIFont systemFontOfSize:Zoom(12)];
            textF.textColor = [RBThemeConfig textBlackColor];
            self.searchTF = textF;
            [searchView addSubview:textF];
            [textF mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(searchImgView.mas_right).offset(Zoom(8));
                make.right.mas_equalTo(-Zoom(18));
            }];
            
            view;
        });
    }
    return _navigationBar;
}

- (RBPagerView *)pagerView
{
    if (!_pagerView)
    {
        self.pagerView = ({
            RBPagerView *view = RBPagerView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.navigationBar.mas_bottom);
            }];
            
            __weak typeof(self) weakSelf = self;
            view.getVcBlock = ^UIViewController * _Nonnull(NSInteger index) {
                return [weakSelf getVcAtIndex:index];
            };
            CGFloat headerStickTop = Zoom(0);
            CGFloat headerH = Zoom(20 + 14 + 14);
            CGFloat headerBottom = Zoom(0);
            [view configWithHeaderStickTop:headerStickTop headerBottom:headerBottom headerHeight:headerH floatViewHeight:headerStickTop + headerH + headerBottom];
            [view setupWithModel:({
                RBPagerHeaderModel *model = RBPagerHeaderModel.new;
                model.titles = @[@"拼团中", @"已成团", @"已失效"];
                model.headerAttributeTitlesForNormalState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.headerAttributeTitlesForSelectedState = ({
                    NSMutableArray *arrayM = NSMutableArray.array;
                    for (NSString *title in model.titles)
                    {
                        [arrayM addObject:[RanTool attributeStringMaker:^(NSMutableArray *maker) {
                            [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig textBlackColor] string:title];
                        }]];
                    }
                    arrayM;
                });
                model.buttonWidthStyle = RBPagerButtonWidthConstValue;
                model.headerButtonWidth = kRBMainScreenWidth/model.titles.count;
                model.showSeperator = NO;
                model.bottomLineColor = [UIColor colorWithHexString:@"#518CFF"];
                model.bottomLineWidth = Zoom(16);
                model.bottomLineHeight = Zoom(6);
                model.bottomLineWidthStyle = RBPagerBottomLineWidthConstValue;
                model.ignoreBottomLineHeight = YES;
                model.bottomLineBottomMargin = Zoom(14);
                model;
            })];
            view.pagerHeaderView.bottomLineView.alpha = 0.4;
            [view.pagerHeaderView.bottomLineView rbSetCornerRadius:Zoom(3)];
            [view.pagerHeaderView.bottomLineView.superview sendSubviewToBack:view.pagerHeaderView.bottomLineView];
            view.floatView.backgroundColor = UIColor.whiteColor;
            view;
        });
    }
    return _pagerView;
}

@end
